package com.chen.springboot.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chen.springboot.common.Result;
import com.chen.springboot.domain.Player;
import com.chen.springboot.domain.Team;
import com.chen.springboot.mapper.mongoMapper.PlayersMapper;
import com.chen.springboot.service.impl.PlayerServiceImpl;
import com.chen.springboot.service.impl.PlayersServiceImpl;
import com.chen.springboot.vo.Players;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author chenchao
 * @date 2022/6/22 14:51
 */
@RestController
@RequestMapping("/player")
@Api(value = "PlayerController",tags = "球员管理")
public class PlayerController {

    @Resource
    private PlayerServiceImpl playerService;


    @RequestMapping("/page")
    @ApiOperation(value = "分页查询")
    public Result selectDict(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "5") Integer pageSize,
                             @RequestParam("") String displayName,
                             @RequestParam("")   String country,
                             @RequestParam("") String position,
                             @RequestParam("") String displayAbbr){

        return Result.success(playerService.selectPage(pageNum, pageSize,displayName,country,position,displayAbbr));
    }


    @PutMapping("/updatePlayer")
    @ApiOperation(value = "更新")
    public Result updatePlayer(@RequestBody Player player){

        return Result.success(playerService.updateById(player));

    }

    @DeleteMapping("/deletePlayer/{id}")
    public Result deletePlayer(@PathVariable("id") int id){
        return Result.success(playerService.removeById(id));
    }


    @PostMapping("/addPlayer")
    @ApiOperation(value = "添加")
    public Result addPlayer(@RequestBody Player player){

        return Result.success(playerService.save(player));

    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        return Result.success(playerService.removeByIds(ids));
    }

    @PostMapping("/addBatch")
    @ApiOperation(value = "爬虫")
    public Result addBatch2(@RequestBody String str)  {
        CopyOnWriteArrayList<Player> copyOnWriteArrayList = new CopyOnWriteArrayList<>();

        JSONArray jsonArray = JSONArray.parseArray(str);
        for (int i = 0; i < jsonArray.size(); i++) {
            Player player=new Player();
            JSONObject jsonObject = JSONObject.parseObject(jsonArray.get(i).toString());
            String playerProfile = jsonObject.getString("playerProfile");
            String teamProfile = jsonObject.getString("teamProfile");
            JSONObject object = JSONObject.parseObject(playerProfile);
//            球队
            JSONObject object2 = JSONObject.parseObject(teamProfile);
//            球员头像
            String playerImg = object.getString("playerId");
//            球员名称
            String displayName = object.getString("displayName");
            String country = object.getString("country");//球员国籍
            String height = object.getString("height");//球员身高
            String weight = object.getString("weight");//球员体重
            String position = object.getString("position");//球员定位
            String displayAbbr = object2.getString("displayAbbr"); //球员所在球队
            player.setPlayerImg("https://res.nba.cn/media/img/players/head/260x190/"+playerImg+".png");
            player.setCountry(country);
            player.setDisplayName(displayName);
            player.setPosition(position);
            player.setDisplayAbbr(displayAbbr);
            player.setHeight(height);
            player.setWeight(weight);
            copyOnWriteArrayList.add(player);
        }
        playerService.saveBatch(copyOnWriteArrayList);

        return Result.success();
    }


}
